package com.arcvideo.acsconnect.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.TextView;

import com.arcvideo.tyingyitonginhouse.R;
import com.framework.core.utils.AppUtil;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author shenmengchao
 * @version 1.0.0
 * @date 2017/7/28
 * @description
 */

public class MySeekBarLayout extends LinearLayout {

    @BindView(R.id.tv_title)
    TextView mTvTitle;
    @BindView(R.id.tv_indicate)
    TextView mTvIndicate;
    @BindView(R.id.seek_bar)
    SeekBar mSeekBar;
    @BindView(R.id.tv_min)
    TextView mTvMin;
    @BindView(R.id.tv_max)
    TextView mTvMax;
    private Context mContext;

    private String mUnit;
    private int mMin = 0;
    private int mMax = 100;
    private String mTitle;
    private int mValue;
    private OnProgressListener mListener;

    public MySeekBarLayout(Context context) {
        this(context, null);
    }

    public MySeekBarLayout(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MySeekBarLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MySeekBarLayout);
        mTitle = typedArray.getString(R.styleable.MySeekBarLayout_title);
        mMin = typedArray.getInt(R.styleable.MySeekBarLayout_min, 0);
        mMax = typedArray.getInt(R.styleable.MySeekBarLayout_max, 100);
        mUnit = typedArray.getString(R.styleable.MySeekBarLayout_unit);
        typedArray.recycle();
        init();
    }

    public void setListener(OnProgressListener listener) {
        this.mListener = listener;
    }

    public int getValue() {
        return mValue;
    }

    private void init() {
        View view = LayoutInflater.from(mContext).inflate(R.layout.layout_setting_seekbar, this, true);
        ButterKnife.bind(this, view);
        mTvTitle.setText(mTitle);
        mTvMin.setText(mMin + mUnit);
        mTvMax.setText(mMax + mUnit);
        mSeekBar.setMax(100);
        mSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                initIndicatePlace(seekBar, progress);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
        initIndicatePlace(mSeekBar, 0);
    }

    private void initIndicatePlace(SeekBar seekBar, int progress) {
        int widthMax = AppUtil.getWindowWidth(mContext) - AppUtil.dp2Px(mContext, 70) - 80;
//        mTvIndicate.setPadding(progress * widthMax / 100 + AppUtil.dp2Px(mContext, 15), 0, 0, 0);
        mValue = progress * (mMax - mMin) / 100 + mMin;
        mTvIndicate.setText(mValue + mUnit);
        if (null != mListener) {
            mListener.onProgressChanged(mValue);
        }
    }

    public void setTitle(String message) {
        if (null != mTvTitle) {
            mTvTitle.setText(message);
        }
    }

    public void setUnit(String unit) {
        this.mUnit = unit;
    }

    public void setMinAndMax(int min, int max) {
        this.mMin = min;
        this.mMax = max;
        mTvMin.setText(min + mUnit);
        mTvMax.setText(max + mUnit);
        mSeekBar.setProgress(0);
        mValue = min;
    }

    public void setProgress(int progress) {
        mSeekBar.setProgress(progress);
//        mValue = progress * (mMax - mMin) / 100 + mMin;
//        initIndicatePlace(mSeekBar, 100);
    }

    public void setValue(int value) {
        if (value >= mMin && value <= mMax) {
            int progress = (value - mMin) * 100 / (mMax - mMin);
            setProgress(progress);
            initIndicatePlace(mSeekBar, progress);
        }
    }

    public interface OnProgressListener {
        void onProgressChanged(int value);
    }
}
